local Msg = require("core.Msg")
local DrawExcel = require("excel.DateActivity").chongzhiDraw
local Grid = require("bag.Grid")
local ActDefine = require("activity.Define")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local MailLogicFL = require("mail.MailLogicFL")
local CowryBagLogic = require("fuli.CowryBagLogic")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local DateActFL = require("activity.DateActFL")
local HtmlUtil = require("common.HtmlUtil")
local Log = require("common.Log")
local OperateChongzhiDraw = require("activity.operate.ChongzhiDraw")

function open(human, id, type)
  local sendMsg = Msg.gc.GC_OPEN_DATE_ACTIVITY
  local activityNum = DateActFL.onGetCurActRuleID(id)    
  local activity = DrawExcel[activityNum]
  if not activity then
    return
  end
  sendMsg.info[0] = #activity.weight
  local cache = {}
  for i=1, #activity.weight do
    sendMsg.info[i].reward[0] = 1
    Grid.create(cache, activity.reward[i][1], activity.reward[i][2])
    Grid.makeItem(cache, sendMsg.info[i].reward[1], 0, human)
    sendMsg.info[i].args[0] = 0
    sendMsg.info[i].canGet = 0
    sendMsg.info[i].name = ""
  end
  sendMsg.myInfo[0] = 3
  sendMsg.myInfo[1] = human.db.dailyChongzhi
  local cnt = math.floor(human.db.dailyChongzhi/activity.canDraw) <= activity.maxDraw and math.floor(human.db.dailyChongzhi/activity.canDraw) or activity.maxDraw
  sendMsg.myInfo[2] = cnt - human.db.YYChongzhiDrawTimes
  sendMsg.myInfo[3] = activity.canDraw
  sendMsg.activityId = id
  sendMsg.type = type or 1
  Msg.send(sendMsg, human.fd)
  local info = {}
  info.type = id
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_DATE_RECORD, info)  
end

function startDraw(human, id)
  local activityNum = DateActFL.onGetCurActRuleID(id)
  local activity = DrawExcel[activityNum]
  if not activity then
    return
  end
  local cnt = math.floor(human.db.dailyChongzhi/activity.canDraw) <= activity.maxDraw and math.floor(human.db.dailyChongzhi/activity.canDraw) or activity.maxDraw 
  local realCnt = cnt - human.db.YYChongzhiDrawTimes
  if realCnt <= 0 then
    return
  end
  human.db.YYChongzhiDrawIndex = 0
  local sendMsg = Msg.gc.GC_DATE_START_DRAW
  local weight = 0
  for i=1, #activity.weight do
    weight = weight + activity.weight[i]
  end
  local rand = math.random(1, weight)
  weight = 0
  for i=1, #activity.weight do
    weight = weight + activity.weight[i]
    if rand <= weight then
      sendMsg.index = i
      sendMsg.multi = 1
      human.db.YYChongzhiDrawIndex = i
      break
    end
  end
  sendMsg.id =id
  Msg.send(sendMsg, human.fd)
  human.db.YYChongzhiDrawTimes = human.db.YYChongzhiDrawTimes + 1  
  
  local item = {}
  item[1] = activity.reward[human.db.YYChongzhiDrawIndex]
  if not BagLogic.checkCanAddItemList(human, item, #item) then
    sendMail(human)
    Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH_SEND_MAIL)
  else
    for i=1, #item do
      BagLogic.addItem(human, item[i][1], 1, item[i][2], "dateactholidaygift")
    end
  end

  human.db.YYChongzhiDrawIndex = 0
  if #item == 0 then
    return
  end  
  local info = {}
  info.type = ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW
  info.id = item[1][1]
  info.cnt = item[1][2]
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_DATE_DRAW, info) 
  open(human, ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW) 
  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_DRAW_END)
  local cache = {}
  Grid.create(cache, item[1][1], item[1][2])
  local str = string.format(Lang.DATE_CHONGZHI_DRAW_BRO, HtmlUtil.fontName(human.name, human._id),  HtmlUtil.fontItem(cache), human._id)
  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  Log.write(Log.LOGID_OSS_YUNYING_CHONGZHI_DRAW, human._id, human.account, human.name,human.db.lv, item[1][1], item[1][2])
  OperateChongzhiDraw.open(human, ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW)
end

function endDraw(human, id)
--  if human.db.YYChongzhiDrawIndex == 0 then
--    return
--  end
--  local activityNum = DateActFL.onGetCurActRuleID(id)
--  local activity = DrawExcel[activityNum]
--  if not activity then
--    return
--  end
--  
--  local item = {}
--  item[1] = activity.reward[human.db.YYChongzhiDrawIndex]
--  if not BagLogic.checkCanAddItemList(human, item, #item) then
--    sendMail(human)
--    Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH_SEND_MAIL)
--  else
--    for i=1, #item do
--      BagLogic.addItem(human, item[i][1], 1, item[i][2], "yunyingAcvity")
--    end
--  end
--
--  human.db.YYChongzhiDrawIndex = 0
--  if #item == 0 then
--    return
--  end  
--  local info = {}
--  info.type = ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW
--  info.id = item[1][1]
--  info.cnt = item[1][2]
--  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_YUNYING_DRAW, info) 
--  open(human, ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW) 
--  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_DRAW_END)
--  local cache = {}
--  Grid.create(cache, item[1][1], item[1][2])
--  local str = string.format(Lang.YUNYING_CHONGZHI_DRAW_BRO, HtmlUtil.fontName(human.name, human._id),  HtmlUtil.fontItem(cache), human._id)
--  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
--  Log.write(Log.LOGID_OSS_YUNYING_CHONGZHI_DRAW, human._id, human.account, human.name,human.db.lv, item[1][1], item[1][2])
end


function sendMail(human)
  if human.db.YYChongzhiDrawIndex == 0 then
    return
  end
  local nGridList = {}
  local activityNum = DateActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW)
  local activity = DrawExcel[activityNum]
  if not activity then
    return
  end
  nGridList[1] = Grid.create(nil, activity.reward[human.db.YYChongzhiDrawIndex][1], activity.reward[human.db.YYChongzhiDrawIndex][2], 1)
  MailLogicFL.CreateNewMailForUser(human._id, "充值抽奖", "由于您的背包已满，特将奖励发给您", nGridList)
  human.db.YYChongzhiDrawIndex = 0
end






